// 运算符
using System;
namespace test
{
    // 定义一个类
    class MyClass {
        public int MyProperty { get; set; }

        // 构造函数
        public MyClass() {
            // 将 MyProperty 的初始值设置为 20
            MyProperty = 20;
        }
    }
    // 运算符
    class A{
        static void Main(string[] args)
        {
            // 算术运算符
            int a = 1,b = 2;
            int c = a + b / a * b-b;
            double d = 5/3;
            float e = 5/2f; // 5/2取整，加f带小数
            decimal f = 2.1m;
            int max = int.MaxValue;
            int min = int.MinValue;
            int what = max + 4;
            
            Console.WriteLine("a + b / a * b-b 的值 {0}",c);
            Console.WriteLine($"The range of integers is {min} to {max}");
            Console.WriteLine($"An example of overflow: {what}");
            Console.WriteLine("5/3的值 {0}",d);
            Console.WriteLine("5/2f的值 {0}",e);
            Console.WriteLine("d + (float)f的值 {0}",d + (double)f);
            Console.WriteLine("d + (float)f的值 {0}",(decimal)d + f);
            Console.WriteLine("e + (float)f的值 {0}",e + (float)f);
            Console.WriteLine("(decimal)e + f的值 {0}",(decimal)e + f);

            // 位运算符
            int bit1 = 5^3;
            int bit2 = 6>>2;
            int bit3 = 5<<2;
            int bit4 = 5&2;
            int bit5 = 5|3;
            int bit6 = ~50;
            Console.WriteLine("5^3的值 {0}",bit1);
            Console.WriteLine("6>>2的值 {0}",bit2);
            Console.WriteLine("5<<2的值 {0}",bit3);
            Console.WriteLine("5&2的值 {0}",bit4);
            Console.WriteLine("5|3的值 {0}",bit5);
            Console.WriteLine("~50的值 {0}",bit6);

            /*其他运算符*/
            // is , GetType
            object obj = e;
            if (obj is string)
            {
                Console.WriteLine("obj is a string.");
            }
            else if (obj is int)
            {
                Console.WriteLine("obj is an int.");
            }
            else
            {
                Console.WriteLine("obj is of an unknown type.");
                Console.WriteLine("The type of obj is: " + obj.GetType().Name);
            }

            // sizeof
            Console.WriteLine("sizeof(int)的值 {0}",sizeof(int));

            // typeof
            Type type = typeof(int);
            Console.WriteLine("typeof(int)的值 {0}",type);

            //  as 运算符用于执行引用类型的显式类型转换, 如果转换成功，则返回转换后的对象；如果转换失败，则返回 null。与强制类型转换不同的是，as 运算符不会引发异常。
            object asobj = new MyClass();
            MyClass myObj = asobj as MyClass;
            if (myObj != null) { // 检查是否转换成功
                Console.WriteLine("转换成功");
                Console.WriteLine("MyProperty 值为：" + myObj.MyProperty + ", asobj: " + asobj);
            } else {
                Console.WriteLine("转换失败");
            }
            // new
            var numArr = new int [3] { 10, 20, 30 };
            Console.WriteLine($"数组内容为: [{string.Join(", ", numArr)}]");

            // ?: ?. ?[] ?? ??= 
            string GetWeatherDisplay(double tempInCelsius) => tempInCelsius < 20.0 ? "Cold." : "Perfect!";
            Console.WriteLine(GetWeatherDisplay(15));  // output: Cold.
            Console.WriteLine(GetWeatherDisplay(27));  // output: Perfect!
        }
    }
}